<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./swiper.css">
  <style>
    .swiper-slide{
    
      background-color: rgb(119, 69, 69);
    }
    .swiper-slide img{
      width: 100%;
    }
    .swiper-slide:nth-child(2n){
      background-color: rgb(163, 187, 57);
    }
    html{
      overflow-x: hidden;
    }
  </style>
</head>

<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.js"></script>
  <script src="./swiper.js"></script>
  <script src="./axios.js"></script>
  <script>
    Vue.component('CommonHead', {
      template: `
        <div>
          <h2>我是公共的头部</h2>  
          <div class="swiper">
            <div class="swiper-wrapper">
              <div class="swiper-slide" v-for="banner in banners" :key="banner.id">
                <img :src="banner.picUrl"/>
              </div>
            </div>
          </div>
        </div>
      `,

      data() {
        return {
          banners: [],
          // 在data中定义初始值 接收 第三方插件 初始化返回的实例 
          swiper: null
        }
      },
      methods: {
        // 请求banner数据
        fetchBanners(){
          // https://api.it120.cc/conner/banner/list
          axios.get('https://api.it120.cc/conner/banner/list').then(res => {
            if(res.data.code === 0) {
              this.banners = res.data.data;
              // 数据请求 banners赋值 banners数据修改了 有数据之后 再初始化一次
              // 重新初始化swiper
              this.$nextTick(() => {
                this.swiper.update();
              })
            }
          })
        },
        // 在methods中定义方法 初始化 插件
        initSwiper() {
          this.swiper = new Swiper('.swiper', {
            autoplay: true,//可选选项，自动滑动
          })

        }
      },
      created(){
        this.fetchBanners();
      },
      mounted() {
        // mounted中调用 初始化函数
        this.initSwiper();
      }
    })
    const vm = new Vue({
      el: '#app'
    })

  </script>
</body>

</html>